package org.liaohailong.library.widget.interact

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.layout_tv_interact.view.*
import kotlinx.android.synthetic.main.layout_tv_interact_content.view.*
import kotlinx.android.synthetic.main.layout_tv_interact_item.view.*
import org.liaohailong.library.R

/**
 * Author: liaohailong
 * Date: 2019/3/11
 * Time: 3:49 PM
 * Description: TV端 聊天互动列表视图
 **/
class TVInteractView(context: Context, attributeSet: AttributeSet?, defaultStyle: Int) :
        FrameLayout(context, attributeSet, defaultStyle), IInteractView.InteractViewCallback {
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context) : this(context, null)

    companion object {
        private const val REMOVE_DURATION = 500L
        private const val ADD_DURATION = 500L
        private const val MIN_ITEM_ALPHA = 0.0f
        private const val MAX_ITEM_ALPHA = 0.54f
    }

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_tv_interact, this, true)
        val linearLayoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)

        val itemAnimator = CustomAlphaItemAnimator(MIN_ITEM_ALPHA, MAX_ITEM_ALPHA)
        itemAnimator.removeDuration = REMOVE_DURATION
        itemAnimator.addDuration = ADD_DURATION
        interact_list.itemAnimator = itemAnimator

        interact_list.layoutManager = linearLayoutManager
        interact_list.adapter = Adapter()
    }

    var onTVInteractViewCallback: OnTVInteractViewCallback? = null
    var isRunning = false

    fun addMessage(message: Message) {
        val adapter = interact_list.adapter as Adapter
        adapter.addData(message)
    }

    fun moveToNext() {
        val adapter = interact_list.adapter as Adapter
        if (adapter.itemCount > 0) {
            isRunning = true
            //当前需要展示的数据
            val currentMessage = adapter.data.removeAt(0)
            //先从列表移除
            adapter.notifyItemRemoved(0)
            //再绘制到内容显示区域
            fadeOut(content_layer, REMOVE_DURATION, object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
                    content_layer.name_txt.text = currentMessage.title
                    interact_container.setMessage(currentMessage, this@TVInteractView)
                    fadeIn(content_layer, ADD_DURATION, object : AnimatorListenerAdapter() {})
                }
            })
        } else {
            isRunning = false
            onTVInteractViewCallback?.onTVInteractMessageComplete()
        }
    }

    class Adapter : RecyclerView.Adapter<ViewHolder>() {
        val data: MutableList<Message> = mutableListOf()
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_tv_interact_item, parent, false)
            return ViewHolder(view)
        }

        override fun getItemCount(): Int {
            return data.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.bindView(data[position], position)
        }

        fun addData(message: Message) {
            data.add(message)
            val position = data.indexOf(message)
            notifyItemInserted(position)
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindView(message: Message, position: Int) {
            //do nothing...
            itemView.alpha = MAX_ITEM_ALPHA
            itemView.name_txt.text = message.title
        }
    }

    fun fadeIn(view: View, duration: Long, listener: Animator.AnimatorListener) {
        val fadeIn = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 1.0f)
        fadeIn.duration = duration
        fadeIn.addListener(listener)
        fadeIn.start()
    }

    fun fadeOut(view: View, duration: Long, listener: Animator.AnimatorListener) {
        val fadeIn = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.0f)
        fadeIn.duration = duration
        fadeIn.addListener(listener)
        fadeIn.start()
    }

    override fun onInteractViewPrepared(message: org.liaohailong.library.widget.interact.Message) {

    }

    override fun onInteractPlayComplete(message: org.liaohailong.library.widget.interact.Message) {
        moveToNext()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        val adapter = interact_list.adapter as Adapter
        adapter.data.clear()
    }

    interface OnTVInteractViewCallback {
        fun onTVInteractMessageComplete()
    }
}